import  Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../components/Login' //登录
import Home from '../components/Home'
/*系统管理员*/
import Index from '../components/Index' //首页
import PersonInfo from '../components/PersonInfo' //个人信息
import Buildings from '../components/Buildings' //楼宇管理
import PowerManage from '../components/PowerManage' //权限管理
import CollegeManage from '../components/CollegeManage' //院部管理
import MajorManage from '../components/MajorManage' //专业管理
import ClassManage from '../components/ClassManage' //班级班里
import StudentManage from '../components/StudentManage' //学生管理
import DormitoryManage from '../components/DormitoryManage' //宿舍管理
import RepairManage from '../components/RepairManage' //报修管理
import ApplyReapirManage from  '../components/ApplyRepairManage' //申请报修
import AuthorityManagement from '../components/AuthorityManagement' //权限列表

//安装路由
Vue.use(VueRouter);

//配置导出路由
const router =  new VueRouter({
  mode: "history",
  routes:[
    { path : '/', redirect: '/login'}, //重定向
    { path: '/login',component: Login},
    { path: '/home', name: 'home', component: Home, redirect: '/index',
      children: [
        { path: '/index', component: Index},
        { path: '/personInfo', component: PersonInfo},
        { path: '/dormitoryManage', component: DormitoryManage},
        { path: '/powerManage', component: PowerManage},
        { path: '/studentManage', component: StudentManage},
        { path: '/class', component: ClassManage},
        { path: '/major', component: MajorManage},
        { path: '/college', component: CollegeManage},
        { path: '/buildingManage', component: Buildings},
        { path: '/repair', component: RepairManage},
        { path: '/applyRepair', component: ApplyReapirManage},
        { path: '/authorityManagement', component: AuthorityManagement}
      ]
    },
]
});

//挂载路由导航守卫
router.beforeEach((to, from, next) =>{
  //next 是一个函数，表示放行
  //next() 放行 next('/login') 强制跳转
  if (to.path === '/login') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem("token");
  //为空跳转掉登录页面
  if (!tokenStr) return next('/login');

  next();

});

export default router
